package testLayer;

import static org.junit.Assert.*;
import modelLayer.ClassRoom;
import modelLayer.Subject;
import modelLayer.Teacher;
import modelLayer.Class;
import modelLayer.TeachingBlock;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import utilLayer.AlreadyExistsException;
import controlLayer.ClassRoomCtr;
import controlLayer.TeachingBlockCtr;
import dbLayer.DBClass;
import dbLayer.DBClassRoom;
import dbLayer.DBSubject;
import dbLayer.DBTeacher;

public class TeachingBlockControllerTest {
	
	private TeachingBlockCtr tbCtr; 
	private boolean retrieveAssociation = false;

	@BeforeClass
	public static void prepareTest() throws Exception {
		
		Teacher t = new DBTeacher().findById(1); 
		new DBClass().insert("men0813", "1", "2013", t);
		new DBClassRoom().insert("L07b", "blank");
	//	new DBSubject().insert("Sport", "blank", t);
		
	}
	
	@AfterClass
	public static void cleanUp() throws Exception {
		
		DBClass dbCl = new DBClass();
		dbCl.remove(dbCl.find("men0813", false).getId());
		new DBClassRoom().remove("L07b");
		new DBSubject().remove("Sport");
		
	}
	
	@Before
	public void setUp() throws Exception {
		tbCtr = new TeachingBlockCtr();
		testCreateTeachingBlock();
		
	}

	@After
	public void tearDown() throws Exception {
		
		testRemoveTeachingBlock();
		
	}

	@Test
	public void testTeachingBlockCtr() {
		
		assertNotNull(this.tbCtr);
		
	}

	@Test
	public void testCreateTeachingBlock() throws Exception {
//		
//		try {
//			Class cl = new DBClass().find("men0813", false);
//			Subject s = new DBSubject().find("Sport", false);
//			ClassRoom cr = new DBClassRoom().find("L07b");
//			int number = 1;
//			assertNotNull(this.tbCtr.createTeachingBlock(cl.getId(), s.getId(), cr.getId(), number));
//		}
//		catch (AlreadyExistsException e) {
//			System.out.println(e);
//		}
//		
	}

	@Test
	public void testFindTeachingBlockByName() {
		
		if (retrieveAssociation){
			assertTrue(this.tbCtr.findTeachingBlockByName("1SpoL07b-1", true) != null &&
					this.tbCtr.findTeachingBlockByName("1SpoL07b-1", true).getClass() != null &&
					this.tbCtr.findTeachingBlockByName("1SpoL07b-1", true).getSubject() != null &&
					this.tbCtr.findTeachingBlockByName("1SpoL07b-1", true).getClassRoom() != null
					);
		}
		else {
			assertNotNull(this.tbCtr.findTeachingBlockByName("1SpoL07b-1", false));
		}
		
	}

	@Test
	public void testFindTeachingBlockById() {
		
		TeachingBlock tb = this.tbCtr.findTeachingBlockByName("1SpoL07b-1", true);
		if (retrieveAssociation){
			assertTrue(this.tbCtr.findTeachingBlockById(tb.getId(), true) != null &&
					this.tbCtr.findTeachingBlockById(tb.getId(), true).getClass() != null &&
					this.tbCtr.findTeachingBlockById(tb.getId(), true).getSubject() != null &&
					this.tbCtr.findTeachingBlockById(tb.getId(), true) != null
					);
		}
		else {
			assertNotNull(this.tbCtr.findTeachingBlockById(tb.getId(), false));
		}
		
	}

	@Test
	public void testGetAll() {
		
		assertTrue(this.tbCtr.getAll(retrieveAssociation).size() > 0);
		
	}

	@Test
	public void testUpdateTeachingBlock() throws Exception {
		
		TeachingBlock tb = this.tbCtr.findTeachingBlockByName("1SpoL07b-1", false); 
		ClassRoom cr = new ClassRoomCtr().findById(1);
		tb.setClassRoom(cr);
		assertTrue(this.tbCtr.updateTeachingBlock(tb) > 0);
		
	}

	public void testRemoveTeachingBlock() {
		
		try {
			TeachingBlock tb = this.tbCtr.findTeachingBlockByName("1SpoL07b-1", false); 
			assertTrue(this.tbCtr.removeTeachingBlock(tb) > 0);
		}
		catch (Exception e) {
			System.out.println(e);
		}
		
	}
	
//	@Test
//	public void testRemoveBySubjectAndClass() throws Exception{
//		Class cl = new DBClass().find("men0813", false);
//		Subject s = new DBSubject().find("Sport", false);
//		try {
//			assertTrue(this.tbCtr.removeByClassAndSubject(cl.getId(), s.getId()) > 0);
//		}
//		catch (Exception e) {
//			System.out.println(e);
//		}
//	}

}
